package com.zzccyy.jpdemo.fragment

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.prolificinteractive.materialcalendarview.CalendarDay
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.base.BaseLazyFragment
import com.zzccyy.jpdemo.bean.ZcBean
import com.zzccyy.jpdemo.dialog.BtDateDialog1
import com.zzccyy.jpdemo.page.AddZcActivity
import com.zzccyy.jpdemo.utils.ListAdapter4
import com.zzccyy.jpdemo.utils.SPUtils


class Fragment3 : BaseLazyFragment(), View.OnClickListener {
    private var tY: TextView? = null
    private var tvPp: TextView? = null
    private var tvYYY: TextView? = null
    private var lllY: LinearLayout? = null
    private var recyclerView: RecyclerView? = null
    private var listAdapter4: ListAdapter4? = null
    private var btDateDialog1: BtDateDialog1? = null

    override fun setLayoutId(): Int {
        return R.layout.fr3
    }

    override fun init(view: View?) {
        tY = view!!.findViewById(R.id.tY)
        lllY = view.findViewById(R.id.lllY)
        tvPp = view.findViewById(R.id.tvPp)
        tvYYY = view.findViewById(R.id.tvYYY)
        recyclerView = view.findViewById(R.id.recyclerView)
        val i1 = view.findViewById<ImageView>(R.id.i1)
        lllY!!.setOnClickListener(this)
        i1.setOnClickListener(this)
    }

    override fun loadData() {
        val today = CalendarDay.today()
        tY!!.text = today.year.toString() + "年"
        val linearLayoutManager = LinearLayoutManager(activity)
        recyclerView!!.layoutManager = linearLayoutManager

        listAdapter4 = ListAdapter4(activity, R.layout.item_fff)
        recyclerView!!.adapter = listAdapter4
        setList()
    }

    private fun setList() {
        val substring = tY!!.text.toString().substring(0, 4)
        val readList = SPUtils.getInstance(activity).getList("list", ZcBean::class.java)
        val list: MutableList<ZcBean.Data> = ArrayList()
        for (i in readList.indices) {
            val zcBean = readList[i]
            if ((zcBean.year.toString() + "") == substring) {
                list.addAll(zcBean.list!!)
            }
        }
        var price = 0.0
        for (i in list.indices) {
            price += list[i].price
        }
        tvPp!!.text = String.format("%.2f", price)
        listAdapter4!!.setNewData(list)

        if (!readList.isEmpty()){
            val maxTime = getMaxTime(list)
            tvYYY!!.text = maxTime
            tvYYY!!.visibility=View.VISIBLE
        }else{
            tvYYY!!.visibility=View.GONE
        }

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.i1 -> {
                //add
                val intent = Intent(activity, AddZcActivity::class.java)
                startActivityForResult(intent, 300)
            }

            R.id.lllY -> {
                if (btDateDialog1 == null) {
                    btDateDialog1 =
                        BtDateDialog1(activity!!, object : BtDateDialog1.OnViewClickListener {
                            override fun setData(data: String?) {
                                tY!!.text = data
                                setList()
                            }
                        })
                }
                btDateDialog1!!.show()
            }
        }
    }
    private fun getMaxTime(list: List<ZcBean.Data>): String? {
        if (list.isEmpty()) {
            return null
        }

        var maxTime = list[0].time
        for (item in list) {
            if (maxTime?.let { item.time!!.compareTo(it) }!! > 0) {
                maxTime = item.time
            }
        }
        return maxTime
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 300 && resultCode == Activity.RESULT_OK) {
            setList()
        }
    }


}